package com.taotao.controller;

import com.taotao.common.resp.ImageResult;
import com.taotao.common.util.QiniuFileUploadUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * Created by zzb on 2017/11/9.
 */
@Controller
@RequestMapping("/pic")
@Slf4j
public class PictureController {

    @Value("${QINIU_HEAD_IMG_BUCKET_URL}")
    private String QINIU_HEAD_IMG_BUCKET_URL;

    ////图片上传 uploadFile与前端一致
    @RequestMapping("/upload")
    @ResponseBody
    public ImageResult picUpload(MultipartFile uploadFile) {
        ImageResult imageResult = new ImageResult();

        try {
            String response = QiniuFileUploadUtil.uploadHeadImg(uploadFile);
            String url = QINIU_HEAD_IMG_BUCKET_URL + "/" + response;
            imageResult.setError(0);
            imageResult.setUrl(url);
        } catch (Exception e) {
            log.error(e.getMessage());
            log.error("上传图片失败!");
            imageResult.setError(1);
            imageResult.setMessage("上传图片失败!");
        }
        return imageResult;
    }

}
